package com.example.librarysystem.Controller; // 包名建议改为小写驼峰，符合Java规范

import com.example.librarysystem.entity.Book;
import com.example.librarysystem.repository.BookRepository;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("/books")
@Tag(name = "图书管理", description = "图书查询相关接口") // Swagger标签
public class BookController {

    @Autowired
    private BookRepository bookRepository;

    /**
     * 图书搜索接口
     * @param isbn ISBN模糊查询
     * @param name 书名模糊查询
     * @return 符合条件的图书列表
     */
    @Operation(
            summary = "图书搜索接口",
            description = "支持通过ISBN或书名进行模糊查询",
            tags = {"图书管理"}
    )
    @GetMapping
    public ResponseEntity<List<Book>> searchBooks(
            @RequestParam(required = false, defaultValue = "") String isbn,
            @RequestParam(required = false, defaultValue = "") String name) {


        List<Book> books = bookRepository.searchBooks(isbn, name);

        // 使用ResponseEntity统一响应格式
        return ResponseEntity.status(HttpStatus.OK)
                .body(books);
    }
}